package com.xiangxiangjd.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiangxiangjd.controller.helper.$;
import com.xiangxiangjd.enums.TopicType;
import com.xiangxiangjd.utils.StringUtilsXiao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.xiangxiangjd.bean.result.ResultPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import com.xiangxiangjd.service.TopicService;
import com.xiangxiangjd.bean.Topic;
import com.xiangxiangjd.controller.model.ApiResultModel;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 肖守凯
 * @since 2021-05-05
 */
@Controller
@RequestMapping("/star/topic")
public class TopicController extends BaseController {


    /**
     * 主页面
     */
    @RequestMapping("admin/main.html")
    public String main() {
        return "Topic/TopicMain";
    }

    /**
     * 添加页面跳转
     *
     * @return 返回页面
     */
    @RequestMapping(value = "admin/addTopicHtml")
    public String addTopicHtml() {
        return "Topic/TopicAdd";
    }

    /**
     * 编辑页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/editTopicHtml")
    public String editTopicHtml(String id, Model model) {
        model.addAttribute("topic", topicService.selectTopicByIdTopic(id));
        return "Topic/TopicEdit";
    }

    /**
     * 查看页面跳转
     *
     * @param id 前端传进的参数
     * @return 返回页面
     */
    @RequestMapping(value = "admin/showTopicHtml")
    public String showTopicHtml(String id, Model model) {
        model.addAttribute("topic", topicService.selectTopicByIdTopic(id));
        return "Topic/TopicShow";
    }


    /**
     * 分页查询Topic
     *
     * @param page  页码
     * @param limit 查几条
     * @param topic 前端传进的参数
     * @return 返回统一分页信息
     */
    @RequestMapping(value = "admin/findTopicByPage")
    @ResponseBody
    public ResultPage findTopicByPage(Integer page, Integer limit, Topic topic) {
        return topicService.findTopicByPage(page, limit, topic);
    }

    /** 添加Topic
     * @param topic  前端传进的参数
     * @return 返回统一信息
     */
//    @RequestMapping(value = "admin/addTopic")
//    @ResponseBody
//    public ApiResultModel saveTopic(Topic topic  ){
//             return topicService.addTopic(topic);
//    }

    /**
     * 编辑Topic
     *
     * @param topic 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/editTopic")
    @ResponseBody
    public ApiResultModel editTopic(Topic topic) {
        return topicService.editTopic(topic);
    }


    /**
     * 删除Topic
     *
     * @param ids 前端传进的ID
     * @return 返回统一信息
     */
    @RequestMapping(value = "admin/delTopic", method = RequestMethod.GET)
    @ResponseBody
    public ApiResultModel delTopic(@RequestParam("datas[]") ArrayList ids) {


        return topicService.delTopics(ids);
    }

    /**
     * 添加 一道试题
     *
     * @param topic 前端传进的参数
     * @return 返回统一信息
     */
    @RequestMapping(value = "/addTopic")
    @ResponseBody
    public ApiResultModel saveTopic(@RequestBody Topic topic) {

        if (StringUtilsXiao.isBlank(topic.getQuestionnaireId())) return $.error("问卷ID 为空，请联系开发人员！");


        //判断该问卷中 试题名称是否 已存在
        List<Topic> getTopicTitleRow = topicService.list(new LambdaQueryWrapper<Topic>()
                .eq(Topic::getTopicTitle, topic.getTopicTitle())
                .eq(Topic::getQuestionnaireId, topic.getQuestionnaireId())
        );
        if (getTopicTitleRow.size() > 0) return $.error("该试题已添加，请修改名称");

        //查询改序号是否存在


        //查看显示条件是不是第一题
        if (topic.getTopicSort().equals(0)&&!StringUtilsXiao.isBlank(topic.getTopicCondition())) return $.error("第一题不能设置显示条件！");


        //查看是不是最后一题
        //   Integer count = topicService.count(new LambdaQueryWrapper<Topic>().eq(Topic::getQuestionnaireId, topic.getQuestionnaireId()));


        return topicService.addTopic(topic);


    }

         /*
                添加显示条件
          */
    @GetMapping(value = "/displayTopic")
    @ResponseBody
    public ApiResultModel displayTopic(@RequestParam(value = "questionnaireId") String questionnaireId, @RequestParam(value = "sort") Integer sort) {





        //获取这个问卷所有的试题 前面的试题
        List<Topic> data=topicService.beforeAllTopic(questionnaireId,sort);

        return $.success(data);


    }


    /**
     * 获取可以跳过的题目
     * <p>
     * sort 当前试题的序号
     */

    @GetMapping(value = "/leapfrogTopic")
    @ResponseBody
    public ApiResultModel leapfrogTopic(@RequestParam(value = "questionnaireId") String questionnaireId, @RequestParam(value = "sort") Integer sort) {


        //获取这个问卷所有的试题 后面的试题
        List<Topic> data = topicService.list(new LambdaQueryWrapper<Topic>()
                .eq(Topic::getQuestionnaireId, questionnaireId)
                .ge(Topic::getTopicSort, sort)
        );

        return $.success(data);


    }


    // 允许上传的文件类型
    private static final Map<String, String> TOPIC_TYPES = new HashMap<>();

    //允许上传的格式
    static {
        TOPIC_TYPES.put(TopicType.DANXUAN, "单选题");
        TOPIC_TYPES.put(TopicType.DUOXUAN, "多选题");
        TOPIC_TYPES.put(TopicType.TIANKONG, "填空题");
        TOPIC_TYPES.put(TopicType.JUZHEN_DANXUAN, "矩阵单选题");
        TOPIC_TYPES.put(TopicType.JUZHEN_DUOXUAN, "矩阵多选题");
        TOPIC_TYPES.put(TopicType.JUZHEN_HUADONG, "矩阵滑动题");
        TOPIC_TYPES.put(TopicType.JUZHEN_LIANGBIAO, "矩阵量表题");
        TOPIC_TYPES.put(TopicType.DUANLUO_SHUOMING, "段落说明");
        TOPIC_TYPES.put(TopicType.DAFEN, "打分题");

    }

    /**
     * 查询是是否可以 引用设置
     *
     * @param questionnaireId 问卷id
     * @param type            当前类型
     * @return
     */

    @GetMapping(value = "/quoteAnswer")
    @ResponseBody
    public ApiResultModel quoteAnswer(@RequestParam(value = "questionnaireId") String questionnaireId, @RequestParam(value = "type") String type, @RequestParam(value = "sort") Integer sort) {
        if (sort.equals(0)) return $.error("第一题不能进行引用设置！");

        //获取这个问卷所有的试题 后面的试题
        List<Topic> data = topicService.getTopicAllAnswer(questionnaireId, type, sort);
        if (data.size() < 1) return $.error("此题钱没有" + TOPIC_TYPES.get(type) + "，无法进行引用设置");


        return $.success(data);


    }

    /**
     * 查看问卷的所有 题目和答案
     *
     * @param questionnaireId 问卷id
     *
     * @return
     */

    @GetMapping(value = "/getQuestionnaireAllTopic")
    @ResponseBody
    public ApiResultModel getQuestionnaireAllTopic(@RequestParam(value = "questionnaireId") String questionnaireId) {


        //获取这个问卷所有的试题 后面的试题
        List<Topic> data = topicService.getQuestionnaireAllTopic(questionnaireId);

        return $.success(data);

    }
}
